//
//  MyAppoinmentViewController.swift
//  iModern
//
//  Created by 立雄 王 on 15/6/30.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class MyOrderFormViewController: FormViewController
{

    private enum Tags: String
    {
        case Button              = "button"
        case sButtonLeftAligned  = "sbuttonLeftAligned"
        case mButtonLeftAligned  = "mbuttonLeftAligned"
        case idButtonLeftAligned = "idButtonLeftAligned"
    }

    var order      = NSDictionary()
    var payManager = PayTransactionManager()

    override func viewDidLoad()
    {

        super.viewDidLoad()
        self.initBackButton()
        self.initializeForm()
    }

    func initializeForm()
    {

        let form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        let status = self.order["status"] as? String
        let datas  = self.order["contact"] as! NSDictionary
        let data   = self.order["advertising"] as! NSDictionary

        form = XLFormDescriptor(title: "")
        section = XLFormSectionDescriptor.formSectionWithTitle("")

        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "alias", rowType: XLFormRowDescriptorTypeText, title: "客户别名:")
        row.disabled = NSNumber(bool: true)
        row.value = datas["alias"] as! String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "fullName", rowType: XLFormRowDescriptorTypeText, title: "客户姓名:")
        row.disabled = NSNumber(bool: true)
        row.value = datas["fullName"] as? String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "cellphone", rowType: XLFormRowDescriptorTypeText, title: "客户电话:")
        row.disabled = NSNumber(bool: true)
        row.value = datas["cellphone"] as? String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "email", rowType: XLFormRowDescriptorTypeText, title: "客户电邮:")
        row.disabled = NSNumber(bool: true)
        row.value = datas["email"] as? String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeText, title: "客户地址:")
        row.disabled = NSNumber(bool: true)
        row.value = datas["address"] as? String
        row.required = true
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("商 品")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: Tags.sButtonLeftAligned.rawValue, rowType: XLFormRowDescriptorTypeButton, title: (NSString(format: "商品名称:%@", (data["summary"] as? String!)!)) as String)
        row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.Left.rawValue
        row.action.formSelector = "buttonLeftAlignedRow:"
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: Tags.mButtonLeftAligned.rawValue, rowType: XLFormRowDescriptorTypeButton, title: (NSString(format: "商品说明:%@", (data["message"] as? String!)!)) as String)
        row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.Left.rawValue
        row.action.formSelector = "buttonLeftAlignedRow:"
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "indexOfSaleTags", rowType: XLFormRowDescriptorTypeText, title: "商品分类:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["indexOfSaleTags"] as? String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "price", rowType: XLFormRowDescriptorTypeText, title: "价格:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["price"] as? Int
        row.required = true
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("客 户")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "message", rowType: XLFormRowDescriptorTypeText, title: "客户备注:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["message"] as? String
        row.required = true
        section.addFormRow(row)

        let id = self.order["id"] as! Int
        row = XLFormRowDescriptor(tag: Tags.idButtonLeftAligned.rawValue, rowType: XLFormRowDescriptorTypeButton, title: NSString(format: "订单号:%@", String(id)) as String)
        row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.Left.rawValue
        row.action.formSelector = "buttonLeftAlignedRow:"
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "startTime", rowType: XLFormRowDescriptorTypeDateTime, title: "时间:")
        row.disabled = NSNumber(bool: true)
        let startTime = self.order["startTime"] as? String
        if startTime == nil
        {
            row.value = NSDate()
        }
        else
        {
            row.value = NSDate.DateFromJSONStringCN(startTime!)
        }
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeText, title: "预约地址:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["address"] as? String
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "status", rowType: XLFormRowDescriptorTypeText, title: "状态:")
        row.disabled = NSNumber(bool: true)
        if status == "Not Started"
        {
            row.value = "订单还未确认"
        }
        if status == "Cancelled"
        {
            row.value = "订单已取消"
        }
        if status == "Started"
        {
            row.value = "订单已经被确认"
        }
        if status == "Completed"
        {
            row.value = "订单已完成"
        }
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "price", rowType: XLFormRowDescriptorTypeText, title: "成交价格:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["price"] as? Int
        row.required = true
        section.addFormRow(row)

        if status == "Not Started"
        {
            section = XLFormSectionDescriptor.formSectionWithTitle("客 户")
            form.addFormSection(section)

            row = XLFormRowDescriptor(tag: "Not Started", rowType: XLFormRowDescriptorTypeButton, title: "确认订单")
            row.action.formSelector = "didTouchCancelChooseButton:"
            section.addFormRow(row)

            row = XLFormRowDescriptor(tag: "Not Started", rowType: XLFormRowDescriptorTypeButton, title: "取消订单")
            row.action.formSelector = "didTouchCancelButton:"
            section.addFormRow(row)
        }

        self.form = form
        self.tableView .reloadData()
    }

    func didTouchCancelChooseButton(sender: XLFormRowDescriptor)
    {
        let rs = payManager.confirmTransactionRecord(self.order["id"] as! Int)
        if rs.statusCode == 200
        {
        }
        self.navigationController?.popViewControllerAnimated(true)
    }

    func didTouchCancelButton(sender: XLFormRowDescriptor)
    {
        let rs = payManager.cancelAppointment(self.order["id"] as! Int)
        if rs.statusCode == 200
        {
        }
        self.navigationController?.popViewControllerAnimated(true)
    }

    func buttonLeftAlignedRow(sender: XLFormRowDescriptor)
    {

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */

}
